﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HalconDotNet;
using CommonLib;
using ElementLib.Interface;

namespace ElementLib
{
    public class Point : IElem
    {
        private double row;
        private string name;
        private double column;
        private static int existCount = 0;
        private HObject region,contour;
        public void Dispose()
        {
            if (contour != null) contour.Dispose();
            if (region != null) region.Dispose();
            contour = null;
            region = null;
        }

        public Point()
        {

        }
        public Point(Point p)
        {
            name = p.name;
            row = p.row;
            column = p.column;
            if (p.region != null)
            {
                region = new HObject(p.region);
            }
            else
            {
                HOperatorSet.GenCircle(out region, row, column, 3);
                HOperatorSet.DilationCircle(region, out region, 10);
            }
            if(p.contour!=null)
            {
                contour = new HObject(p.contour);
            }
            else
            {
                HOperatorSet.GenContourRegionXld(region, out contour, "border");
            }
             
        }
        public void GetRowColumn(out HTuple row, out HTuple column)
        {
            row = this.row;
            column = this.column;
        }
        public Point(double row, double column,string name)
        {
            this.name = name; 
            this.row = row;
            this.column = column;
            HOperatorSet.GenCircle(out region, this.row, this.column, 3);
            HOperatorSet.GenContourRegionXld(region, out contour, "border");
            HOperatorSet.DilationCircle(region, out region, 10);
        }

        public Point(double row, double column)
        {
            this.row = row;
            this.column = column;
            HOperatorSet.GenCircle(out region, this.row, this.column, 3);
            HOperatorSet.GenContourRegionXld(region, out contour, "border");
            HOperatorSet.DilationCircle(region, out region, 10);
        }

        public double Distance(Point point)
        {
            double distance = 0;
            distance = Math.Pow(this.row - point.row, 2) + Math.Pow(this.column - point.column, 2);
            return Math.Sqrt(distance);
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public double Row
        {
            get
            {
                return row;
            }
            set { row = value; }
        }

        public HObject Contour
        {
            get { return contour; }
        }

        public HObject Region
        {
            get { return region; }
        }
        public double Column
        {
            get { return column; }
            set { column = value; }
        }

        public override string ToString()
        {
            return "点:" + name + "(" + Row.ToString("f1") + "," + Column.ToString("f1") + ")" ;
        }

        public void Reset(int num = 0)
        {
            existCount = 0;
        }

        public void ApplyChange()
        {
            this.name = "POINT" + existCount++;
        }
        public void dispROI(HTuple windowHandle,string color)
        {
            HOperatorSet.SetColor(windowHandle, color);
            HOperatorSet.SetDraw(windowHandle,"fill");
            HObject roi = null;
            HOperatorSet.GenRegionContourXld(contour, out roi, "filled");
            HOperatorSet.DispObj(roi, windowHandle);
        }


    }
}
